<?php
namespace app\Services;




use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Psr\SimpleCache\InvalidArgumentException;

class MioServices
{

    private static $app_url = 'https://api.publisher.miratsquanto.com/api/v1/publisher/';

    private static $web_api_ser = 'U1uwElzxQSfJZmjGkcHD6haSpJatO8hEyII';

    private static $sec_phone = 'mqJXtwubypTyXhKuEpKHiUM5YPT9ewngx5jtbdk6';

    /**
     * @throws GuzzleException
     * @throws InvalidArgumentException
     */
    public static function surveys()
    {
        $client = new Client([
            // Base URI is used with relative requests
            'base_uri' => self::$app_url,
            // You can set any number of default request options.
            'timeout'  => 10.0,
            'headers' => [
                'publisher-authentication-key' => self::$sec_phone
            ]
        ]);

        $data = $client->post('surveys',[
            'json'=>[
                'authorizationKey'=>self::$web_api_ser,
                'country'=>'US',
                'page'=>'1',
                'surveyGroupExists'=>false,
                "incidenceRateGTE"=> 25,
                "collectPII"=>false,
                "lengthOfInterviewGTE"=> 10,
                "lengthOfInterviewLTE"=> 30,
                'CPIGTE'=>0.4
            ]
        ])->getBody()->getContents();
        $data_je_data = json_decode($data,true);
        $insert_data = [

        ];


        if($data_je_data['success']){
           return $data_je_data['surveys'];
        }
    }

    public static function renshe($three_id = '')
    {
//        if(Cache::hasCache('MIO_IDS_'.$three_id)){
//            return Cache::getCacheValue('MIO_IDS_'.$three_id);
//        }
        $url = 'surveys/'.$three_id.'/qualification';
        $client = new Client([
            // Base URI is used with relative requests
            'base_uri' => self::$app_url,
            // You can set any number of default request options.
            'timeout'  => 10.0,
            'headers' => [
                'publisher-authentication-key' => self::$sec_phone
            ]
        ]);

        $data = $client->post($url,[
            'json'=>[
                'authorizationKey'=>self::$web_api_ser,
            ]
        ])->getBody()->getContents();
        $data_je_data = json_decode($data,true);
//        Cache::setCache('MIO_IDS_'.$three_id,$data_je_data,Carbon::now()->addMinutes(60));
        return $data_je_data;
    }



    public static function url_get($survey_id = 0)
    {
//        if($survey_id == 0){
//            self::error('不存在');
//        }
//        $three_info = SurveysDao::getSurveysBuilderQuery(['survey_id'=>$survey_id]);
//        if(!$three_info->exists()){
//            self::error('不存在');
//        }
//        $three_info = $three_info->first()->toArray();
        $client = new Client([
            // Base URI is used with relative requests
            'base_uri' => self::$app_url,
            // You can set any number of default request options.
            'timeout'  => 10.0,
            'headers' => [
                'publisher-authentication-key' => self::$sec_phone
            ]
        ]);
//
        $data = $client->post('surveys/'.$survey_id.'/link',[
            'json'=>[
                'authorizationKey'=>self::$web_api_ser
            ]
        ])->getBody()->getContents();
        $data_je_data = json_decode($data,true);
        if(!isset($data_je_data['liveLink'])){
            self::error('报错:请联系技术');
        }
        return $data_je_data['liveLink'];
    }
}